﻿using System;
using System.Windows.Forms;
using DevelopersToolbox.Properties;

namespace DevelopersToolbox {
    public partial class MainForm : Form {
        private readonly PluginLoader _pluginLoader = PluginLoader.Instance;

        public MainForm() {
            InitializeComponent();
            ReloadPlugins();
            Visible = false;
            ShowInTaskbar = false;
            WindowState = FormWindowState.Minimized;
        }

        private void ReloadPlugins() {
            _tabs.TabPages.Clear();
            if (Settings.Default.SelectedPluginPages != null) {
                foreach (SelectedPluginPage spp in Settings.Default.SelectedPluginPages) {
                    IPage page = _pluginLoader.GetPage(spp.PluginAssemblyQualifiedName, spp.PageGuid);
                    if (page != null) {
                        TabPage tabPage = new TabPage(page.Name);
                        tabPage.Controls.Add(page.Control);
                        page.Control.Dock = DockStyle.Fill;
                        _tabs.TabPages.Add(tabPage);
                    }
                }
            }
        }

        private void MainForm_Resize(object sender, EventArgs e) {
            if (WindowState == FormWindowState.Minimized) {
                Visible = false;
                ShowInTaskbar = false;
            } else {
                Visible = true;
                ShowInTaskbar = true;
            }
        }

        private void _notifyIcon_DoubleClick(object sender, EventArgs e) {
            Visible = true;
            WindowState = FormWindowState.Normal;
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e) {
            Visible = true;
            WindowState = FormWindowState.Normal;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
            Close();
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e) {
            Close();
        }

        private void executeToolStripMenuItem_Click(object sender, EventArgs e) {
            IExecutableTool tool = GetExecutableToolFromCurrentTab();
            if (tool != null) {
                tool.Execute();
            }
        }

        private IExecutableTool GetExecutableToolFromCurrentTab() {
            TabPage selectedTab = _tabs.SelectedTab;
            if (selectedTab == null) {
                return null;
            }
            foreach (Control control in selectedTab.Controls) {
                if (control is IExecutableTool) {
                    return (IExecutableTool)control;
                }
            }
            return null;
        }

        private void _tabs_SelectedIndexChanged(object sender, EventArgs e) {
            IExecutableTool tool = GetExecutableToolFromCurrentTab();
            if (tool == null) {
                _executeToolStripMenuItem.Enabled = false;
            } else {
                _executeToolStripMenuItem.Enabled = true;
            }
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e) {
            OptionsDialog dlg = new OptionsDialog();
            dlg.ShowDialog(this);
            ReloadPlugins();
        }
    }
}
